#ifndef _TUNLENT_H_
#define _TUNLENT_H_

#include "list.h"
#include "tunnel.h"

static list_t pending_tunlent;

typedef struct _tunnel_entrance {
    int   sock;
    tunl_t tunl;
    struct _tunnel_entrance *pair;
    list_t node;
} tunlent_t;

static inline void init_tunlent(void)
{
    list_init(&pending_tunlent);
}

static inline void add_tunlent(tunlent_t *ent)
{
    list_insert(&pending_tunlent, &ent->node);
}

static inline void del_tunlent(tunlent_t *ent)
{
    list_remove(&ent->node);
}

static inline tunlent_t *find_pair_tunlent(tunlent_t *src)
{
    tunlent_t *dst = NULL;
    int pair_active = (src->tunl.active == TUNL_REQ_ACTIVE_YES) ? TUNL_REQ_ACTIVE_NO : TUNL_REQ_ACTIVE_YES;

    list_t *n;
    list_for_each(n, &pending_tunlent) {
        dst = list_entry(n, tunlent_t, node);
        if ((dst->tunl.number == src->tunl.number) && (dst->tunl.active == pair_active)) {
            return dst;
        }
    };
    return NULL;
}

#endif
